package com.yaoxun.bored.common;

import javax.inject.Inject;

import org.apache.shiro.SecurityUtils;
import org.springframework.validation.BindingResult;

import com.yaoxun.bored.model.sys.user.User;
import com.yaoxun.bored.util.enums.JsonStatus;
import com.yaoxun.bored.validator.ValidatorHelper;

public class BaseController {

	@Inject
	protected ValidatorHelper validatorHelper;
	
	public Integer getLoginUserId() {
		return getLoginUser().getUserId();
	}
	
	public User getLoginUser() {
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		return user;
	}
	
	/**
	 * 使用hibernate-validator校验实体对象
	 * @param obj
	 * @return true 没有错误，false 有错误
	 */
	public boolean commValidate(Object obj, JsonObject jsonObject) {
		BindingResult bindingResult = validatorHelper.validate(obj);
		if(bindingResult.hasErrors()) {
			jsonObject.setStatusMsg(JsonStatus.FAIL, bindingResult.getFieldError().getDefaultMessage());
			return false;
		}
		return true;
	}
	
}
